/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.wtk.skin.terra;

import wtkx.Component;
import wtkx.Display;
import wtkx.SplitPane;
import wtkx.wtk.ComponentMouseListener;
import wtkx.wtk.ComponentMouseButtonListener;
import wtkx.wtk.Cursor;
import wtkx.wtk.Dimensions;
import wtkx.wtk.Mouse;
import wtkx.wtk.Orientation;
import wtkx.wtk.Point;
import wtkx.wtk.Span;
import wtkx.wtk.SplitPaneListener;
import wtkx.wtk.Theme;
import wtkx.wtk.skin.ComponentSkin;
import wtkx.wtk.skin.SplitPaneSkin;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;

/**
 * Split pane skin.
 *
 * @author tvolkert
 * @author jdp
 */
public class TerraSplitPaneSkin
    extends SplitPaneSkin
    implements SplitPaneListener
{

    /**
     * Split pane splitter component.
     *
     * @author tvolkert
     * @author jdp
     */
    protected class Splitter
        extends SplitPaneSkin.Splitter
    {
        public Splitter() {
            super();
            this.setSkin(new TerraSplitPaneSkin.SplitterSkin());
        }
    }

    /**
     * Split pane splitter component skin.
     *
     * @author tvolkert
     * @author jdp
     */
    protected class SplitterSkin
        extends SplitPaneSkin.SplitterSkin
    {

        public SplitterSkin(){
            super();
        }

        public void paint(Graphics2D graphics) {
            SplitPane splitPane = (SplitPane)TerraSplitPaneSkin.this.getComponent();

            Orientation orientation = splitPane.getOrientation();

            int width = getWidth();
            int height = getHeight();

            int imageWidth, imageHeight;
            if (orientation == Orientation.HORIZONTAL) {
                imageWidth = width - 4;
                imageHeight = Math.min(height - 4, 8);
            } else {
                imageWidth = Math.min(width - 4, 8);
                imageHeight = height - 4;
            }

            if (imageWidth > 0 && imageHeight > 0) {
                int translateX = (width - imageWidth) / 2;
                int translateY = (height - imageHeight) / 2;
                graphics.translate(translateX, translateY);

                Color dark = splitterHandlePrimaryColor;
                Color light = splitterHandleSecondaryColor;

                if (orientation == Orientation.HORIZONTAL) {
                    graphics.setStroke(new BasicStroke());

                    graphics.setPaint(dark);
                    graphics.drawLine(0, 0, imageWidth - 1, 0);
                    graphics.drawLine(0, 3, imageWidth - 1, 3);
                    graphics.drawLine(0, 6, imageWidth - 1, 6);

                    graphics.setPaint(light);
                    graphics.drawLine(0, 1, imageWidth - 1, 1);
                    graphics.drawLine(0, 4, imageWidth - 1, 4);
                    graphics.drawLine(0, 7, imageWidth - 1, 7);
                } else {
                    int half = imageHeight / 2;

                    graphics.setPaint(dark);
                    graphics.fillRect(0, 0, 2, half);
                    graphics.fillRect(3, 0, 2, half);
                    graphics.fillRect(6, 0, 2, half);

                    graphics.setPaint(light);
                    graphics.fillRect(0, half, 2, half);
                    graphics.fillRect(3, half, 2, half);
                    graphics.fillRect(6, half, 2, half);
                }
            }
        }
    }

    private Splitter splitter = new Splitter();

    private Color splitterHandlePrimaryColor;
    private Color splitterHandleSecondaryColor;


    public TerraSplitPaneSkin() {
        super();
        TerraTheme theme = (TerraTheme)Theme.getTheme();
        splitterHandlePrimaryColor = theme.getColor(9);
        splitterHandleSecondaryColor = theme.getColor(10);
    }


    @Override
    public void install(Component component) {
        super.install(component);

        SplitPane splitPane = (SplitPane)component;
        splitPane.getSplitPaneListeners().add(this);

        splitPane.add(splitter);
        splitter.updateCursor();
    }

    @Override
    public void uninstall() {
        SplitPane splitPane = (SplitPane)getComponent();
        splitPane.getSplitPaneListeners().remove(this);

        splitPane.remove(splitter);

        super.uninstall();
    }

    protected void resizeSplitter(int x, int y, int w, int h){
        this.splitter.setLocation(x, y);
        this.splitter.setSize(w, h);
    }

    public Color getSplitterHandlePrimaryColor() {
        return splitterHandlePrimaryColor;
    }

    public void setSplitterHandlePrimaryColor(Color splitterHandlePrimaryColor) {
        if (splitterHandlePrimaryColor == null) {
            throw new IllegalArgumentException("splitterHandlePrimaryColor is null.");
        }

        this.splitterHandlePrimaryColor = splitterHandlePrimaryColor;
        splitter.repaint();
    }

    public final void setSplitterHandlePrimaryColor(String splitterHandlePrimaryColor) {
        if (splitterHandlePrimaryColor == null) {
            throw new IllegalArgumentException("splitterHandlePrimaryColor is null.");
        }

        setSplitterHandlePrimaryColor(decodeColor(splitterHandlePrimaryColor));
    }

    public Color getSplitterHandleSecondaryColor() {
        return splitterHandleSecondaryColor;
    }

    public void setSplitterHandleSecondaryColor(Color splitterHandleSecondaryColor) {
        if (splitterHandleSecondaryColor == null) {
            throw new IllegalArgumentException("splitterHandleSecondaryColor is null.");
        }

        this.splitterHandleSecondaryColor = splitterHandleSecondaryColor;
        splitter.repaint();
    }

    public final void setSplitterHandleSecondaryColor(String splitterHandleSecondaryColor) {
        if (splitterHandleSecondaryColor == null) {
            throw new IllegalArgumentException("splitterHandleSecondaryColor is null.");
        }

        setSplitterHandleSecondaryColor(decodeColor(splitterHandleSecondaryColor));
    }

    public void orientationChanged(SplitPane splitPane) {
        splitter.updateCursor();
        super.orientationChanged(splitPane);
    }

    public void primaryRegionChanged(SplitPane splitPane) {
        splitter.updateCursor();
        super.primaryRegionChanged(splitPane);
    }

    public void lockedChanged(SplitPane splitPane) {
        splitter.updateCursor();
        super.lockedChanged(splitPane);
    }
}
